package ce.com.cenewbluesdk.entity.k2;

import android.os.Parcel;
import android.os.Parcelable;


import java.util.TimeZone;

import ce.com.cenewbluesdk.CEBC;
import ce.com.cenewbluesdk.entity.BaseData;
import ce.com.cenewbluesdk.entity.CEDevData;
import ce.com.cenewbluesdk.uitl.ByteUtil;
import ce.com.cenewbluesdk.uitl.TimeUtil;

/**
 * Created by Administrator on 2015/7/2.
 *
 * 当前的绝对时间 带了时区
 *
 */
public class K2_CESyncTime extends BaseData implements Parcelable {

    byte[] abs_time = new byte[4]; //绝对时间 s
    byte[] offset_time = new byte[4]; //时区偏移时间 s

    public K2_CESyncTime(long absLong, long offsetLong) {
        abs_time = ByteUtil.intToByte((int) (absLong / 1000));
        offset_time = ByteUtil.intToByte4((int) (offsetLong / 1000));
    }

    protected K2_CESyncTime(Parcel in) {
        abs_time = in.createByteArray();
        offset_time = in.createByteArray();
    }

    public static final Creator<K2_CESyncTime> CREATOR = new Creator<K2_CESyncTime>() {
        @Override
        public K2_CESyncTime createFromParcel(Parcel in) {
            return new K2_CESyncTime(in);
        }

        @Override
        public K2_CESyncTime[] newArray(int size) {
            return new K2_CESyncTime[size];
        }
    };

    public static K2_CESyncTime getCurrentTime() {
        long now = TimeUtil.now();
        return new K2_CESyncTime(now, TimeZone.getDefault().getOffset(now));
    }

    public byte[] getBytes() {
        byte[] bytes = new byte[abs_time.length + offset_time.length];
        System.arraycopy(abs_time, 0, bytes, 0, offset_time.length);
        System.arraycopy(offset_time, 0, bytes, 4, 4);
        return bytes;
    }

    public static int getItemSize() {
        return 8;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByteArray(abs_time);
        dest.writeByteArray(offset_time);
    }
    @Override
    public CEDevData toCEDevData() {
        CEDevData data=new CEDevData();
        data.setCmd(CEBC.K2.CMD_TYPE_SEND);
        data.setDataType(CEBC.K2.DATA_TYPE_TIME);
        data.setData(getBytes());
        data.setItemL(8);
        data.setItemNumber(1);
        return data;
    }
}
